package com.cyanite.playlv2.util;

import android.os.Parcel;
import android.text.TextUtils;

import com.cyanite.playlv2api.types.Group;
import com.cyanite.playlv2api.types.Tip;
import com.cyanite.playlv2api.types.Venue;

public class VenueUtils {
	
    public static void addTip(Venue venue, Tip tip) {
		if (venue.getTips() == null) {
			venue.setTips(new Group<Tip>());
		}
		
        for (Tip it : venue.getTips()) {
            if (it.getId().equals(tip.getId())) {
            	return;
            }
        }

        venue.getTips().add(tip);
    }
    
    public static void replaceTipsAndTodos(Venue venueTarget, Venue venueSource) {
    	if (venueTarget.getTips() == null) {
    		venueTarget.setTips(new Group<Tip>());
    	}
    	if (venueSource.getTips() == null) {
    		venueSource.setTips(new Group<Tip>());
    	}
    	
    	venueTarget.getTips().clear();
    	venueTarget.getTips().addAll(venueSource.getTips());
    }
    
    public static boolean getSpecialHere(Venue venue) {
	    if (venue != null && venue.getSpecials() != null && venue.getSpecials().size() > 0) {
			Venue specialVenue = venue.getSpecials().get(0).getVenue();
	        if (specialVenue == null || specialVenue.getId().equals(venue.getId())) {
	        	return true;
	        }
		}
	    
	    return false;
    }
    
    /**
     * Creates a copy of the passed venue. This should really be implemented
     * as a copy constructor.
     */
    public static Venue cloneVenue(Venue venue) {
    	Parcel p1 = Parcel.obtain();
    	Parcel p2 = Parcel.obtain();
    	byte[] bytes = null;
    	
    	p1.writeValue(venue);
    	bytes = p1.marshall();
    	
    	p2.unmarshall(bytes, 0, bytes.length);
        p2.setDataPosition(0);
        Venue venueNew = (Venue)p2.readValue(Venue.class.getClassLoader());
    	
        p1.recycle();
        p2.recycle();
        
        return venueNew;
    }
    
    public static String toStringVenueShare(Venue venue) {
        StringBuilder sb = new StringBuilder();
        sb.append(venue.getName()); sb.append("\n");
        sb.append(StringFormatters.getVenueLocationFull(venue));
        if (!TextUtils.isEmpty(venue.getPhone())) {
            sb.append("\n");
            sb.append(venue.getPhone());
        }
        return sb.toString();
    }
    
    /**
     * Dumps some info about a venue. This can be moved into the Venue class.
     */
    public static String toString(Venue venue) {
    	StringBuilder sb = new StringBuilder();
    	sb.append(venue.toString()); sb.append(":\n");
    	sb.append("  id:       "); sb.append(venue.getId()); sb.append("\n");
    	sb.append("  name:     "); sb.append(venue.getName()); sb.append("\n");
    	sb.append("  address:  "); sb.append(venue.getAddress()); sb.append("\n");
    	sb.append("  cross:    "); sb.append(venue.getCrossstreet()); sb.append("\n");
    	sb.append("  hastodo:  "); sb.append(venue.getHasTodo()); sb.append("\n");
    	sb.append("  tips:     "); sb.append(venue.getTips() == null ? "(null)" : venue.getTips().size()); sb.append("\n");
    	sb.append("  specials: "); sb.append(venue.getSpecials() == null ? "(null)" : venue.getSpecials().size()); sb.append("\n");
    	
    	return sb.toString();
    }
}
